package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baidu.ueditor.ActionEnter;
import com.sl.au.entity.BussinessContent;
import com.sl.au.entity.BussinessInspection;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.User;
import com.sl.au.service.BussinessContentService;
import com.sl.au.service.BussinessLevelService;
import com.sl.utils.MsgHelper;


/**
 * 百度编辑器Controller
 * 
 * @author yangdi
 *
 */

@RestController
@RequestMapping(value = "/ui/ueditor")
public class UEController extends BaseController {

	@Autowired
	BussinessLevelService bussinessLevelService;
	
	@Autowired
	BussinessContentService bussinessContentService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("ueditor/ueindex");
		return modelAndView;
	}
	
	@RequestMapping(value = "/controller", method = {RequestMethod.POST,RequestMethod.GET})
	public String controller(HttpServletRequest request, HttpServletResponse response){
		response.setContentType("application/json");  
        String rootPath = request.getSession().getServletContext().getRealPath("/");
		String json = new ActionEnter( request, rootPath ).exec();
		return json;
	}
	
	//显示商户的所有节点和商户图片ID
	@RequestMapping(value="getBussinessLevelAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getBussinessLevelAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<BussinessLevel> node=null;
		node=bussinessLevelService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=bussinessLevelService.getNodeMap(node);
		map.put("code", 1);
		map.put("nodes", nodeMap);
		return map;
	}
	
	@RequestMapping(value="/setBussinessContent",method = RequestMethod.POST)
	public MsgHelper setBussinessContent(@RequestBody Map<String,Object> map){
		int code = 0;
		String msg = "";
		String bussinessLevelId = (String) map.get("bussinessLevelId");
		String content = (String) map.get("content");
		BussinessLevel bussinessLevel =  bussinessLevelService.getRepository().findOne(bussinessLevelId);
		if(bussinessLevel != null){
			BussinessContent bussinessContent = new BussinessContent();
			
			BussinessContent bussinessContentOld = bussinessContentService.getRepository().findBussinessContentByBussinessLevel(bussinessLevel.getId());
			//BussinessContent bussinessContentOld = null;
			if (bussinessContentOld != null ) {
				bussinessContentOld.setBussinessContent(content);
				bussinessContentService.saveBussinessContent(bussinessContentOld);
			}else{
				bussinessContent.setBussinessLevel(bussinessLevel);
				bussinessContent.setBussinessContent(content);
				bussinessContentService.saveBussinessContent(bussinessContent);
			}
			code = 0;
			msg = "商户区域描述保存成功！";
		}else{
			code = 1;
			msg = "商户区域描述保存失败！";
		}
		MsgHelper msgHelper = new MsgHelper(code,msg);
		return msgHelper;
	}
	
	@RequestMapping("/getBussinessContent/{id}")
	public Map<String,Object> getBussinessContent(@PathVariable("id") String id) {
		//MsgHelper msgHelper = new MsgHelper();
		Map<String,Object> map = new HashMap<String,Object>();
		int code = 0;
		BussinessContent bussinessContent = bussinessContentService.getRepository().findBussinessContentByBussinessLevel(id);
		if(bussinessContent != null){
			code = 0;
			map.put("bussinessContent", bussinessContent);
		}else {
			code = 1;
		}
		map.put("code",code);
		return map;
	}
}
